<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/18
 * Time: 14:43
 */
class common
{
    public function mc_notice_init(){
        global $_W;
        if(empty($_W['account'])) {
            $_W['account'] = uni_fetch($_W['uniacid']);
        }
        if(empty($_W['account'])) {
            return error(-1, '创建公众号操作类失败');
        }
        $acc = WeAccount::create();
        if(is_null($acc)) {
            return error(-1, '创建公众号操作对象失败');
        }
        return $acc;
    }
    public function createImage($image){
        load()->func('file');
        if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $image, $result)){
            $type = $result[2];
            $filename = "images/jianbao/".date('Y/m/d')."/".time()."_".random(6).".".$type;
            if(file_write($filename, base64_decode(str_replace($result[1],'',$image)))){
                $return = tomedia($filename);
            }
        }else{
            $return = tomedia($image);
        }
        return $return;
    }
    public function createVoice($voice){
        load()->func('file');
        if(preg_match('/^(data:\s*audios\/(\w+);base64,)/', $voice, $result)){
            $type = $result[2];
            $filename = "audios/jianbao/".date('Y/m/d')."/".time()."_".random(6).".".$type;
            if(file_write($filename, base64_decode(str_replace($result[1],'',$voice)))){
                $return = tomedia($filename);
            }
        }else{
            $return = tomedia($voice);
        }
        return $return;
    }
}